package ui.activity.mine;

import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;

import net.ck.hzq.R;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import Utils.SharedPrefStore;
import base.BaseAct;
import butterknife.BindView;
import event.NetErrorEvent;
import model.PersonalCenterBean;
import ui.activity.mine.biz.PayPwdBiz;
import ui.activity.mine.component.DaggerPayPwdComponent;
import ui.activity.mine.contract.PayPwdContract;
import ui.activity.mine.module.PayPwdModule;
import ui.activity.mine.presenter.PayPwdPresenter;
import util.StringUtils;
import widget.ClearEditText;


/**
 * 设置提现密码
 * Created by chenjian on 2017/8/4.
 */

public class PayPwdAct extends BaseAct implements PayPwdContract.View {


    @Inject
    PayPwdPresenter presenter;
    @Inject
    PayPwdBiz biz;
    @BindView(R.id.pay_pwd_phone)
    TextView phone;
    @BindView(R.id.pay_pwd_code)
    ClearEditText code;
    @BindView(R.id.pay_pwd_get_code)
    TextView getCode;
    @BindView(R.id.pay_pwd)
    ClearEditText pwd;
    @BindView(R.id.pay_pwd_show)
    ImageView pwdShow;
    @BindView(R.id.pay_pwd_submit)
    Button submit;

    private CountDownTimer timer;


    @Override
    protected void setRoot() {
//        setTitleBar(R.color.colorBlue);
        setContentView(R.layout.activity_pay_pwd);
    }

    @Override
    protected void setup() {
        DaggerPayPwdComponent.builder().payPwdModule(new PayPwdModule(this)).build().inject(this);
        setToolBar(getResources().getString(R.string.lang_pay_pwd), "");
        presenter.setBiz(biz);
    }


    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        SharedPrefStore store = new SharedPrefStore();
        presenter.getUserInfo(store.getString("id", "0"));
        RxView.clicks(getCode)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.getCode());
        RxView.clicks(pwdShow)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.setPwd(pwd.getTransformationMethod() == HideReturnsTransformationMethod.getInstance()));
        RxView.clicks(submit)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.submitData(phone.getText().toString().trim(), pwd.getText().toString().trim(), code.getText().toString().trim()));
    }

    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        biz.onDestory();
        if (timer != null) {
            timer.cancel();
        }
    }

    @Override
    public void upDatePwd(boolean isShow) {
        if (isShow) {
            pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
            pwdShow.setBackgroundResource(R.mipmap.pay_close);
        } else {
            pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            pwdShow.setBackgroundResource(R.mipmap.pay_open);
        }
    }

    @Override
    public void upDataCode() {
        if (timer != null) {
            timer.cancel();
        }
        getCode.setEnabled(false);
        timer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long l) {
                getCode.setText("      " + l / 1000 + "秒      ");
                getCode.setTextColor(Color.WHITE);
                getCode.setBackgroundResource(R.drawable.btn_bg_blue);

            }

            @Override
            public void onFinish() {
                getCode.setEnabled(true);
                getCode.setText("获取验证码");
                getCode.setTextColor(Color.parseColor("#0CA6F0"));
                getCode.setBackgroundResource(R.drawable.bg_share_blue);
            }
        };
        timer.start();
    }

    @Override
    public void upDateUser(PersonalCenterBean.DataBean bean) {
        try {
            if (!TextUtils.isEmpty(bean.getCustom().getPhoneNumber())) {
                phone.setText(StringUtils.replacePhone(bean.getCustom().getPhoneNumber()));
            }
        } catch (Exception e) {
            phone.setText(bean.getCustom().getPhoneNumber());
        }
    }

}
